// pages/mch/clerk/list.js
const App = getApp()
import Util from '../../../utils/util'

Component({
  options: {
    addGlobalClass: true,
  },
  properties: {

  },
  data: {
    init: false,
    loading: false,
    CustomBar: App.Global.CustomBar,
    items: [],
    itemsNum:null,

    modal: '',
    nclk: {},
    activeNames: ['1'],
    come:false

  },

  lifetimes: {
    attached() {
      wx.sub('ReloadClerkList', this, () => {
        this.execLoad()
      })
      this.execLoad()
    },
    detached() {
      wx.cancel('ReloadClerkList', this)
    },
  },
  methods: {
    showButton(e){
      console.log(e.currentTarget.dataset,"3333")
      const {index} = e.currentTarget.dataset;
      const items = [...this.data.items]
      const item = items[index]
      item.showMore = ! item.showMore
      this.setData({items})
    },
    onChange(event) {
      this.setData({
        activeNames: event.detail,
      });
    },
    execLoad() {
      this.setData({
        loading: true
      })
      App.doPost({
        uri: '/app/merchant/clerk/list',
        body: {

        },
        fnSucc: resp => {
          const bigItem = resp.data.items
          // const proItem = bigItem.map(item=>({
          //   ...item,
          //   showMore:false
          // }))
          bigItem.forEach(items=>{
            items.showMore=false
          })
          this.setData({
            init: true,
            loading: false,
            items: bigItem
          })
        }
      })
    },
    toIncome(e) {
      let clk = e.currentTarget.dataset.clk
      wx.navigateTo({
        url: `/pages/mch/clerk/income?cid=${clk.id}`,
      })
    },
    doReset(e) {
      console.log("safdsgefgrt")
      let clk = e.currentTarget.dataset.clk
      if (clk.id == App.Global.User.id) {
        Util.Toast.fail('无法重置自己账号')
        return
      }

      Util.Dialog.confirm(`是否重置店员[${clk.name}]微信绑定`, () => {
        this.execReset(clk)
      })
    },
    execReset(clk) {
      this.setData({
        loading: true
      })
      App.doPost({
        uri: '/app/merchant/clerk/reset',
        body: {
          id: clk.id
        },
        fnSucc: (resp) => {
          clk.bind.pwd = resp.data.code
          wx.navigateTo({
            url: '/pages/funcs/share',
            success: (res) => {
              res.eventChannel.emit('accept', {
                type: 'clerk_invite',
                info: {
                  clk: clk
                }
              })
            }
          })
        }
      })
    },
    doDelete(e) {
      let clk = e.currentTarget.dataset.clk
      if (clk.id == App.Global.User.id) {
        Util.Toast.fail('无法将自己移出门店')
        return
      }
      if (clk.admin) {
        Util.Toast.fail('无法将管理员移出门店')
        return
      }
      if (clk.manager) {
        Util.Toast.fail('无法将店长移出门店')
        return
      }
      Util.Dialog.confirm(`是否将店员[${clk.name}]移出门店`, () => {
        this.execDelete(clk.id)
      })
    },
    execDelete(id) {
      this.setData({
        loading: true
      })
      App.doPost({
        uri: '/app/merchant/clerk/remove',
        body: {
          id: id
        },
        fnSucc: () => {
          this.execLoad()
        }
      })
    },
    doSetManager(e) {
      let clk = e.currentTarget.dataset.clk
      if (clk.manager) {
        Util.Toast.fail('该店员已是店长身份')
        return
      }
      Util.Dialog.confirm(`是否将店员[${clk.name}]设为店长身份`, () => {
        this.execSetManager(clk.id)
      })
    },
    execSetManager(id) {
      this.setData({
        loading: true
      })
      App.doPost({
        uri: '/app/merchant/clerk/manager',
        body: {
          id: id
        },
        fnSucc: () => {
          this.execLoad()
        }
      })
    },
    hideModal() {
      this.setData({
        modal: ''
      })
    },
  }
})